package com.qcby.shujia.demo.service.impl;

import com.qcby.shujia.demo.entity.Role;
import com.qcby.shujia.demo.entity.Role_Right;
import com.qcby.shujia.demo.entity.User_Role;
import com.qcby.shujia.demo.entity.vo.RoleVo;
import com.qcby.shujia.demo.mapper.RoleMapper;
import com.qcby.shujia.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: RoleServiceImpl
 * @description: TODO 类描述
 * @author: whz
 * @date: 2021/9/20
 **/
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public int deleteById(Integer id) {
        int i=roleMapper.deleteById(id);
        roleMapper.deleterr(id);
        roleMapper.deleteur(id);
        return i;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> li=roleMapper.selectAll();
        return li;
    }

    @Override
    public int deletedisp(int[] ids) {
        int flag=1;
        int j;
        for (int i = 0; i <ids.length ; i++) {
            j=deleteById(ids[i]);
            if(j==0){
                flag=0;
            }
        }
        return flag;
    }

    @Override
    public RoleVo selectRrById(Integer id) {
        RoleVo roleVo = roleMapper.selectRrById(id);
        return roleVo;
    }

    @Override
    public int insert(Role role, List<Integer> rightids) {
        int i=roleMapper.insert(role);
        int roleid=role.getId();
        for (int j = 0; j <rightids.size() ; j++) {
            Role_Right role_right = new Role_Right();
            role_right.setRoleid(roleid);
            role_right.setRightid(rightids.get(j));
            roleMapper.insertrr(role_right);
        }
        return i;
    }

    @Override
    public int update(Role role, List<Integer> uprightids) {
        int i = roleMapper.update(role);
        roleMapper.deleterr(role.getId());
        for (int j = 0; j < uprightids.size(); j++) {
            Role_Right role_right = new Role_Right();
            role_right.setRoleid(role.getId());
            role_right.setRightid(uprightids.get(j));
            roleMapper.insertrr(role_right);
        }
        return i;
    }
}
